package xvalidate

import (
	"errors"
	"gitee.com/xiedongji/antgo/core"
	"gitee.com/xiedongji/antgo/cpts/xlog"
	"net/http"
)

const defaultMemory = 32 << 20

type any interface{}

type IForm struct {
}

func (t IForm) Name() string {
	return "iform"
}

// 解析请求,绑定数据对象 i=ComIdReq
func (t IForm) Bind(req *http.Request, obj interface{}) error {

	if err := req.ParseForm(); err != nil {
		return err
	}

	if err := req.ParseMultipartForm(defaultMemory); err != nil && !errors.Is(err, http.ErrNotMultipart) {
		return err
	}

	xlog.Logger(core.AppLogName, "web").Debug().Any("Form:", req.Form).Msg("")

	if err := mapForm(obj, req.Form); err != nil {
		return err
	}

	return nil
}
